package com.changgou.order.service.impl;

import com.changgou.order.dao.TaskHisMapper;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import com.changgou.order.pojo.TaskHis;
import com.changgou.order.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: HuangFu
 * @Date: 2020/2/18 10:17
 * @Description:
 */
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskHisMapper taskHisMapper;

    @Override
    public void addTask(Task task) {
        Date date = new Date();
        task.setCreateTime(date);
        task.setUpdateTime(date);
        taskMapper.insertSelective(task);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delTask(Task task) {
        //校验数据
        if(task == null){
            return;
        }
        Long taskId = task.getId();
        task.setId(null);
        task.setDeleteTime(new Date());
        TaskHis taskHis = new TaskHis();
        //对象拷贝
        BeanUtils.copyProperties(task,taskHis);
        //新增任务记录
        taskHisMapper.insertSelective(taskHis);

        task.setId(taskId);
        //删除任务
        taskMapper.deleteByPrimaryKey(task);
        System.out.println("订单服务完成了添加历史任务并删除原有任务的操作");
    }
}
